<?php
namespace Home\Controller;
header("Content-Type:text/html;charset=utf-8");
use Think\Controller;
class IndexController extends CommonController {
    public function _before_index(){
    	if(!session('user')){
    		if(!session('webisopens')){
                session('[start]');
                session('webisopens',null);
                session('webisopens', 1);
    			$this->redirect('Home/Index/index');
    		}
    	}else{
    		if(!session('webisopens')){
                session('[start]');
                session('webisopens',null);
                session('webisopens', 1);
    			$this->redirect('Home/Index/main');
    		}
    	}
    }

    public function index(){
    	if(!session('user')){
    		$this->display('index');
    	}else{
    		$this->redirect('Home/Index/main');
    	}
    }

    public function login(){
    	if(empty($_POST)){
    		echo 0;
    		exit;
    	}else{
    		$input_code = $_POST['code'];
    		$input_email = $_POST['email'];
    		$session_code = session('logincode');
    		$input_pass = md5($_POST['password']);

    		if($input_code != $session_code){
    			echo 0;
    			exit;
    		}else{

    			$UserM = M('User');
    			$isuser = $UserM->where("user_email='$input_email'")->find();
    			$UserM = M('PermissionGroup');
    			$permission_group = $isuser['permission_group'];
    			$user_permission_group = $UserM->where("id='$permission_group'")->find();
            	if($input_pass != $isuser['login_password']){
            		echo '-1';
            		exit;
            	}else{
            		session('[start]');
            		session('logincode',null);
                	/* 用户session */
                	session('user',null);
                	session('user', $isuser);
                	/* 用户权限组session */
                	session('user_permission_group',null);
                	session('user_permission_group', $user_permission_group);
            		echo '1';
            		exit;
            	}
    		}
    	}
    }

    public function send_code(){

    	if(empty($_POST) && empty($_GET)){
    		echo "0";
    		return "0";
    		exit;
    	}else{
    		$title = C('WEBTITLE')."-登录验证码";
    		$name = C('WEBTITLE');

    		if(empty($_POST)){
    			$toaddress = $_GET['toaddress'];
    		}else{
    			$toaddress = $_POST['toaddress'];
    		}

    		$UserM = M('User');
    		$isuser = $UserM->where("user_email='$toaddress'")->find();
    		if(count($isuser) <= 0){
    			echo "2";
    			exit;
    		}else{
    			$code = rand(pow(10,(6-1)), pow(10,6)-1);

    			$permission_group = $isuser['permission_group'];
    			$UserM = M('Permission_group');
    			$user_permission_group = $UserM->where("id='$permission_group'")->find();

    			$Body = '<div style="width:670px;margin:0 auto;padding: 0px;font-size: 12px;line-height: 1.5;"><div style="height: 200px;"><img src="http://openapi.zcyso.com/phpmailer/images/email_title.jpg" style="vertical-align: bottom;"  /></div><div style="background:url(http://openapi.zcyso.com/phpmailer/images/email_content_bkgd.png);"><div style="padding: 30px 40px 10px 40px;margin: 0px 20px 0px 20px;"><p style="font-family:&#39;微软雅黑&#39;;font-size:14px;color:#4f4f4f;">Hi，<span style="color:#259fdb;font-weight:bold">'.$toaddress.'</span>！</p></div><div style="margin: 0px 20px 0px 20px;padding: 0 40px 0 40px;text-indent: 2em;"><p style="font-family:&#39;微软雅黑&#39;;font-size:14px;color:#4f4f4f;">您的账号正在尝试登录，要继续，请复制下方验证码并输入。（30分钟内有效）</p></div><div style="padding:30px 0 20px 0;text-align: center;"><a style="width:220px;height:60px;line-height:60px;font-size:16px;font-weight:bold;text-align: center;text-decoration:none;color:#1ABC9C;display:inline-block;background-color:#EEF6F9;border-radius:5px;">'.$code.'</a></div></div><div style="height: 30px;background: url(http://openapi.zcyso.com/phpmailer/images/email_buttom.png);"></div><div style="text-align: center;color: #a5a5a5;font-size:10px">MHS<br  />这是一封由系统自动发出的通知邮&#x4EF6;，无需您回复。您的意见可以发送到amh@zcyso.com</div></div>';

    			$postUrl = 'http://openapi.zcyso.com/phpmailer/index.php';
    			$post_data['title'] = $title;
    			$post_data['name'] = $name;
    			$post_data['Body'] = $Body;
    			$post_data['toaddress'] = $toaddress;
                $ch = curl_init();//初始化curl
                curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
                curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
                curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                $sendis = curl_exec($ch);//运行curl
                curl_close($ch);
                if($sendis == 1 || $sendis == "1"){
                	session('[start]');

                	/* 验证码session */
                	session('logincode',null);
                	session('logincode', $code, 1800);

                	echo "1";
                	exit;
                }else{
                	echo "0";
                	exit;
                }
            }
        }
    }

    public function login_out(){
    	session('user', null);
    	session('user_permission_group', null);
    	$this->redirect('index');
    }

    public function main(){
    	$this->sysinfos();
    	$countusers = M('User');
    	$countuser = $countusers->where('user_type = 1')->count();
    	$countproxy = $countusers->where('user_type = 2')->count();
    	$countmoney = $countusers->sum('balance');
        session('[start]');
        session('logincode',null);
    	$user['user'] = session('user');
    	$user['user_permission_group'] = session('user_permission_group');
    	$this->assign('user',$user);
    	$this->assign('countuser',$countuser);
    	$this->assign('countproxy',$countproxy);
    	$this->assign('countmoney',$countmoney);
    	$this->assign('server',session('server', $_SERVER));
    	$this->display('main');
    }

    public function userlist(){
        $UserM = M('User');
        $userlist = $UserM->select();
        $this->assign('userlist',$userlist);
        $this->assign('ACTION_NAME',ACTION_NAME);
        $this->display('userlist');
    }

    public function hostinglist(){
        $HostingM = M('Hosting');
        $hostlist = $HostingM->select();
        $this->assign('hostlist',$hostlist);
        $this->assign('ACTION_NAME',ACTION_NAME);
        $this->display('hostinglist');
    }

    public function hostingadd(){
        // $HostingM = M('Hosting');
        // $hostlist = $HostingM->select();
        // $this->assign('hostlist',$hostlist);
        $this->assign('ACTION_NAME',ACTION_NAME);
        $this->assign('INTEGRATED_WEB_SYS',C('INTEGRATED_WEB_SYS'));
        $this->display('hostingadd');
    }

    public function deleteHost(){
        if(empty($_POST) || empty($_POST['hostingid'])){
            echo "0";
        }else{
            $hostingid = $_POST['hostingid'];
            $HostingM = M('Hosting');
            $isdelete = $HostingM->where('id='.$hostingid)->delete();
            if($isdelete > 0){
                    echo "1";
                    exit;
            }else{
                    echo "0";
                    exit;
            }
        }
    }

    public function intwebsys()
    {
        $this->assign('ACTION_NAME',ACTION_NAME);
        $this->display('intwebsys');
    }

    public function systemconf()
    {
        $webtitle = C('WEBTITLE');
        $this->assign('webtitle',$webtitle);
        $this->assign('ACTION_NAME',ACTION_NAME);
        $this->display('systemconf');
    }

    public function setwebconf(){
        $siteinfo_file = CONF_PATH.'system.conf.php';
        if(file_exists($siteinfo_file)){
            if(IS_POST){
                $setdata = var_export(I('post.'), true);
                $systemconf = '$systemconf';
                $result = file_put_contents($siteinfo_file, "<?php \n $systemconf = ".$setdata.';');
                if($result){
                    echo 1;
                }else{
                    echo 0;
                }
            }
        }else{
            echo 2;
        }
    }

}